/*
 * www.unisinsight.com Inc.
 * Copyright (c) 2019 All Rights Reserved.
 */
package com.fengwuj.mutils.date;

import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;

/**
 * 修订记录：
 * jiangbo 2020/4/18 14:02 创建
 *
 * @author jiangbo
 */
@Slf4j
public class DateUtils {
    private static final String DATE_FORMAT_ALL = "yyyy-MM-dd HH:mm:ss";
    private static final String DATE_FORMAT_MIN = "yyyy-MM-dd HH:mm";

    /**
     * 对象转Date
     * @param date Long:时间戳 || Date
     * @return
     */
    public static Date object2Date(Object date){
        Date tempDate = null;
        if (date instanceof Date){
            tempDate = (Date) date;
        }
        if (date instanceof Long){
            tempDate = new Date((Long)date);
        }
        if (date instanceof String){
            throw new RuntimeException();
        }
        return tempDate;
    }

    public static String showTime(Object time){
        Date tempDate = object2Date(time);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_FORMAT_MIN);
        if (Objects.isNull(tempDate)){
            tempDate = new Date();
        }
        return simpleDateFormat.format(tempDate);
    }

    /**
     * 清除年月日
     * @param date
     * @return
     */
    public static Date clearDate(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.clear(Calendar.YEAR);
        calendar.clear(Calendar.MONTH);
        calendar.set(Calendar.DATE, 1);
        return calendar.getTime();
    }

    /**
     * 清除时分秒
     * @param date
     * @return
     */
    public static Date clearTime(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获取当前日期的最大时间值（当天）
     * @param date
     * @return
     */
    public static Long change2CurrentDayMax(Object date){
        //清空时间
        Date tempDate = clearTime(object2Date(date));
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(tempDate);
        //当前时间日期+1，秒数-1
        calendar.add(Calendar.DAY_OF_MONTH,1);
        calendar.add(Calendar.SECOND,-1);
        return calendar.getTimeInMillis();
    }
}
